/**
 * 根据当前路径获取 ws 路径
 */
export function getWsLocation(uri) {
  return window.location.origin.replace("http", "ws") + uri;
}

/**
 * 时间转换 秒-> 易读的年月日时分秒
 */

export function secondToDateStr(msd) {
  var time = msd;
  if (time != null && time !== "") {
    if (time > 60 && time < 60 * 60) {
      time =
        parseInt(time / 60.0) +
        "分钟" +
        parseInt((parseFloat(time / 60.0) - parseInt(time / 60.0)) * 60) +
        "秒";
    } else if (time >= 60 * 60 && time < 60 * 60 * 24) {
      time =
        parseInt(time / 3600.0) +
        "小时" +
        parseInt((parseFloat(time / 3600.0) - parseInt(time / 3600.0)) * 60) +
        "分钟" +
        parseInt(
          (parseFloat(
            (parseFloat(time / 3600.0) - parseInt(time / 3600.0)) * 60
          ) -
            parseInt(
              (parseFloat(time / 3600.0) - parseInt(time / 3600.0)) * 60
            )) *
            60
        ) +
        "秒";
    } else if (time >= 60 * 60 * 24) {
      time =
        parseInt(time / 3600.0 / 24) +
        "天" +
        parseInt(
          (parseFloat(time / 3600.0 / 24) - parseInt(time / 3600.0 / 24)) * 24
        ) +
        "小时" +
        parseInt((parseFloat(time / 3600.0) - parseInt(time / 3600.0)) * 60) +
        "分钟" +
        parseInt(
          (parseFloat(
            (parseFloat(time / 3600.0) - parseInt(time / 3600.0)) * 60
          ) -
            parseInt(
              (parseFloat(time / 3600.0) - parseInt(time / 3600.0)) * 60
            )) *
            60
        ) +
        "秒";
    } else {
      time = parseInt(time) + "秒";
    }
  }
  return time;
}

export function uuid(len, radix) {
  var chars = "0123456789abcdefghijklmnopqrstuvwxyz".split("");
  var uuid = [];
  radix = radix || chars.length;
  len = len || 32;
  for (var i = 0; i < len; i++) {
    uuid[i] = chars[0 | (Math.random() * radix)];
  }
  return uuid.join("");
}

export function deepClone(obj) {
  var newObj = obj instanceof Array ? [] : {};
  for (var i in obj) {
    newObj[i] = typeof obj[i] === "object" ? deepClone(obj[i]) : obj[i];
  }
  return newObj;
}

// 判断浏览器类型，safari上需要点击播放音频
export function isSafari() {
  var isChrome = navigator.userAgent.indexOf("Chrome") > 0;
  var isSafari = navigator.userAgent.indexOf("Safari") > 0;
  if (isSafari) {
    if (isChrome) {
      // console.log("谷歌浏览器");
      return false;
    } else {
      console.log("是Safari环境");
      return true;
    }
  } else {
    return false;
  }
}

// 将秒转化成hh:mm:ss形式
export function formatSeconds(result) {
  var h =
    Math.floor(result / 3600) < 10
      ? "0" + Math.floor(result / 3600)
      : Math.floor(result / 3600);
  var m =
    Math.floor((result / 60) % 60) < 10
      ? "0" + Math.floor((result / 60) % 60)
      : Math.floor((result / 60) % 60);
  var s =
    Math.floor(result % 60) < 10
      ? "0" + Math.floor(result % 60)
      : Math.floor(result % 60);
  return h + ":" + m + ":" + s;
}

// 讲汉子转化为字节
export function getByteLen(val) {
  var len = 0;
  for (var i = 0; i < val.length; i++) {
    var a = val.charAt(i);
    // eslint-disable-next-line no-control-regex
    if (a.match(/[^\x00-\xff]/gi) != null) {
      len += 2;
    } else {
      len += 1;
    }
  }
  return len;
}
